﻿using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.Goods;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Goods
{
    public class BTB_SupplierBusiness : BaseBusiness<BTB_Supplier>, IBTB_SupplierBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public BTB_SupplierBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<BTB_Supplier>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<BTB_Supplier>();
            var search = input.Search;


            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<BTB_Supplier, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }
        public async Task<BTB_Supplier> GetTheDataAsync(string id)
        {

            return await GetEntityAsync(id);
        }
        public async Task<List<BTB_Supplier>> GetAll()
        {
            return await GetIQueryable().ToListAsync();
        }
        public async Task AddDataAsync(BTB_Supplier data)
        {
            BTB_Supplier data1 = new BTB_Supplier()
            {
                id = GuidHelper.GenerateKey(),
                CreatorId = _operator.UserId,
                Name = data.Name,
                Address = data.Address,
                SupplierLogo = data.SupplierLogo,
                Phone = data.Phone,
                PhoneName = data.PhoneName,
                one = data.one,
                two = data.two,
                CreateTime = data.CreateTime,
                Editor = _operator.UserId,
                EditTime = data.EditTime,
                Deleted = false,
                counts = data.counts,
                Accuracy = data.Accuracy,
                Latitude = data.Latitude
            };
            await InsertAsync(data1);
        }

        public async Task UpdateDataAsync(BTB_Supplier data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion
        #region 前台展示
        public List<BTB_Supplier> GetTheDataName(string where)
        {
            return GetGoodsSearch(q => q.Address.Contains(where));
        }
        #endregion

        #region 私有成员

        #endregion
    }
}